package server

import (
	"github.com/fvbock/endless"
	"github.com/gin-contrib/pprof"

	"mychat/internal/common/logger"
	"mychat/internal/conf"
	"mychat/internal/data"
	"mychat/internal/service"
)

type Server struct {
	bs  *conf.Bootstrap
	lg  logger.Logger
	dt  *data.Data
	svc *service.Service
}

func NewServer(bs *conf.Bootstrap, lg logger.Logger) *Server {

	dt := data.NewData(bs.Data, lg)
	svc := service.NewService(bs, lg, dt)
	return &Server{
		bs:  bs,
		lg:  lg,
		dt:  dt,
		svc: svc,
	}
}

func (s *Server) Run() error {
	router := s.initRouter()
	pprof.Register(router)

	addr := s.bs.Server.Http.Addr
	err := endless.ListenAndServe(addr, router)
	if err != nil {
		return err
	}
	return nil
}
